using SmartCommunity.Models;
using Mapster;
using System.ComponentModel.DataAnnotations;

namespace SmartCommunity.WebApi.Services.System
{
    #region DTO属性映射
    /// <summary>
    /// 属性映射 DictDtoRegister
    /// </summary>
    public class DictDtoRegister : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            config.ForType<DictEntity, DictDto>().Map(d => d.DictItems, s => s.DictItems.Select(a => a.Adapt<DictItemDto>()), s => s.DictItems != null);

            config.ForType<DictItemEntity, DictItemDto>().Map(d => d.DictType, s => s.Dict.DictType, s => s.Dict != null);
            config.ForType<DictItemEntity, DictItemDto>().Map(d => d.DictName, s => s.Dict.DictName, s => s.Dict != null);
        }
    }
    #endregion

    #region 数据传输对象 DictDto
    /// <summary>
    /// 数据传输对象：数据字典
    /// </summary>
    public class DictDto : BaseDto
    {
        /// <summary>
        /// 字典ID
        /// </summary>
        public int DictId { get; set; }

        /// <summary>
        /// 字典类别
        /// </summary>
        [Required(ErrorMessage = "字典类别不能为空")]
        [StringLength(50, ErrorMessage = "字典类别 长度不能超过 50.")]
        public string DictType { get; set; } = string.Empty;

        /// <summary>
        /// 字典名称
        /// </summary>
        [Required(ErrorMessage = "字典名称不能为空")]
        [StringLength(50, ErrorMessage = "字典名称 长度不能超过 50.")]
        public string DictName { get; set; } = string.Empty;

        /// <summary>
        /// 备注说明
        /// </summary>
        [StringLength(255, ErrorMessage = "备注说明 长度不能超过 255.")]
        public string? Remark { get; set; }

        public IEnumerable<DictItemDto> DictItems { get; set; } = Enumerable.Empty<DictItemDto>();
    }
    #endregion

    #region 数据查询对象
    /// <summary>
    /// 查询模型：数据字典
    /// </summary>
    public class DictQuery : AbstractQuery<DictEntity>
    {
        /// <summary>
        /// 主键ID多选查询
        /// </summary>
        public IEnumerable<int> Ids { get; set; } = [];

        /// <summary>
        /// 根据查询条件添加查询表达式
        /// </summary>
        protected override void MergeQueryExpression()
        {
            AddExpression(Ids.Any(), e => Ids.Contains(e.DictId));
            AddSearchExpression(s => o => o.DictName.Contains(s) || o.DictType.Contains(s));
            AddDateExpression(start => e => e.CreatedTime >= start, end => e => e.CreatedTime <= end);
        }
    }
    #endregion
}
